<?php #20071102 http://www.framework.altervista.org/FrameWork/applications/03_download/02_current_version/application.inc
define('APPCURRENTVERSION','1.00');


/************************************************************************/
/* FrameWork  test preview                                              */
/* ==================================================================== */
/*                                                                      */
/* This program is free software. You can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License.       */
/************************************************************************/


loadlib("libArchivedir");

$dirwork="datas/$path/$apptitle";

if (!file_exists($dirwork))mkdir($dirwork,0755);

//-----------------------------------------------------------------------------------------------------------------------

// make the zip from the site datas: ready to clone the site
function makezip($dirwork,$nome_file){

    $fd=opendir($dirwork);
    while (false !== ($nf= readdir($fd))){
	if ($nf[0]!='.' && (substr($nf,-13) != substr($nome_file,-13)) ){
	    unlink("$dirwork/$nf");
	    echo "deleting:$dirwork/$nf <br />";
	}
    }
    closedir($fd);

    if (!file_exists("$dirwork/$nome_file")){

	$zipfile= new zip_file("$dirwork/$nome_file");
	$zipfile->set_options(array('inmemory'=>"0",'overwrite'=>1,'storepaths','prepend','level','type'=>"zip"));


	$dir = new DeepDir();
	$dir->setDir("applications");
	$dir->load();
	foreach($dir->files as $nf){
	    $zipfile->add_files($nf);
	}
	echo "applications...done <br />";

	$dir = new DeepDir();
	$dir->setDir("blocks");
	$dir->load();
	foreach($dir->files as $nf){
	    $zipfile->add_files($nf);
	}
	echo "blocks...done <br />";

	$dir = new DeepDir();
	$dir->setDir("extras");
	$dir->load();
	foreach($dir->files as $nf){
	    $zipfile->add_files($nf);
	}
	echo "extras...done <br />";

	$dir = new DeepDir();
	$dir->setDir("libs");
	$dir->load();
	foreach($dir->files as $nf){
	    $zipfile->add_files($nf);
	}
	echo "libs...done <br />";

	$dir = new DeepDir();
	$dir->setDir("js");
	$dir->load();
	foreach($dir->files as $nf){
	    $zipfile->add_files($nf);
	}
	echo "js...done <br />";

	$dir = new DeepDir();
	$dir->setDir("themes");
	$dir->load();
	foreach($dir->files as $nf){
	    $zipfile->add_files($nf);
	}
	echo "themes...done <br />";

	$zipfile->add_files("index.php");
	$zipfile->add_files("libMain.js");
	$zipfile->add_files("ahah-load.gif");
	$zipfile->add_files("upgrade.inc");

	echo "core...done <br />";

	$zipfile->create_archive();
    }

    $count=0;
    if (file_exists("$dirwork/.count")) $count=join(file("$dirwork/.count"));
    $count++;
    $f=fopen("$dirwork/.count","w");
    fwrite($f,$count);
    fclose($f);


    echo "<hr />";
    echo "scarica <a href='$dirwork/$nome_file'>$nome_file</a>\n";
    echo "<p align='right'> [<a href='index.php?appname=download&subapp=02_current_version'>continua</a>]</p>\n";
}
//-----------------------------------------------------------------------------------------------------------------------
//-----------------------------------------------------------------------------------------------------------------------

// ftp instal of current version
function makeftp($dirwork,$nome_file){
    $server=$_POST['server'];
    $path=$_POST['path'];
    $user=$_POST['user'];
    $password=$_POST['password'];


    $id=ftp_connect($server);
    $login=ftp_login($id,$user,$password);

    if ($id && $login){
	ftp_mkdir($id,$path);
	ftp_chdir($id,$path);

	ftp_mkdir($id,"applications");
	ftp_copy($id,"applications/");
	echo "applications...done <br />";

	ftp_mkdir($id,"blocks");
	ftp_copy($id,"blocks/");
	echo "blocks...done <br />";

	ftp_mkdir($id,"extras");
	ftp_copy($id,"extras/");
	echo "extras...done <br />";

	ftp_mkdir($id,"libs");
	ftp_copy($id,"libs/");
	echo "libs...done <br />";

	ftp_mkdir($id,"js");
	ftp_copy($id,"js/");
	echo "js...done <br />";


	ftp_mkdir($id,"themes");
	ftp_copy($id,"themes/");
	echo "themes...done <br />";

	ftp_put( $id,"index.php","index.php", FTP_BINARY );
	ftp_put( $id,"libMain.js","libMain.js", FTP_BINARY );
	ftp_put( $id,"ahah-load.gif","ahah-load.gif", FTP_BINARY );
	ftp_put( $id,"upgrade.inc","upgrade.inc", FTP_BINARY );
    }

    ftp_quit($id);
    echo "<p align='right'> [<a href='index.php?appname=download&subapp=02_current_version'>continua</a>]</p>\n";
}

function ftp_copy($id,$path){
	chdir("$path");
	ftp_chdir($id,"$path");
	$fd=opendir("./");
	while (false !== ($nf= readdir($fd))){
	    if ($nf[0]!='.'){
		if (is_dir($nf)){
		    ftp_mkdir($id,$nf);
		    //ricorsione
		    ftp_copy($id,$nf);
		}
		else{
		    //echo "$nf <br/>";
		    ftp_put( $id,$nf,$nf, FTP_BINARY );
		}
	    }
	}
	chdir("../");
	ftp_chdir($id,"../");
}
//-----------------------------------------------------------------------------------------------------------------------
?>
        <h3>current version</h3><hr />
	<p>
<?php

$op=$_GET['op'];
$nome_file="FW-".date("Y.m.d-H.00").".zip";

switch ($op){
    case "makezip":
	makezip($dirwork,$nome_file);
	break;
    case "makeftp":
	makeftp($dirwork,$nome_file);
	break;
    default:
?>

<form action="index.php?appname=download&subapp=02_current_version&op=makezip" method="post">
    <input type='submit' value=' Download zip file of current version' /> <b>Download counter</b> <?=@join(@file("$dirwork/.count"));?>
</form>
<hr />
<?php if(extension_loaded('ftp')){ ?>
	<form action="index.php?appname=download&subapp=02_current_version&op=makeftp" method="post">
		server host<input type='text' name='server' /> path value <input type='text' name='path' /><br /><br />
		user name  <input type='text' name='user' /> password <input type='password' name='password' /><br /><br />
		<input type='submit' value=' ftp install of current version' /> <b>install counter</b> <?=@join(@file("$dirwork/.ftp_count"));?>
	</form>
<?php } ?>

<?php
    break;
    }
?>
</p>
